package com.luyuan.crm.sale.web.provider;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.SelectItem;

import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.Customer;
import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.member.model.MemberCoupon;
import com.luyuan.crm.member.model.MemberCouponType;
import com.luyuan.crm.member.service.MemberCouponService;
import com.luyuan.crm.sale.model.Order;
import com.ruijie.foundation.dao.support.HibernateUtils;
import com.ruijie.web.jsf.context.WizardScope;
import com.ruijie.web.jsf.provider.SelectItemsProvider;

@Component("couponDeductionProvider")
public class CouponDeductionProvider implements SelectItemsProvider {
    @Resource(name = "memberCouponService")
    private MemberCouponService memberCouponService;

    @Override
    public List<SelectItem> getSelectItems(Object... arg0) {
        Order order = (Order) WizardScope.get("order");
        Customer customer = (Customer) WizardScope.get("customer");
        MemberCoupon oldCoupon = (MemberCoupon) WizardScope.get("order.couponDeduction");

        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, ""));
        if (!(HibernateUtils.resolveRealObject(customer) instanceof Member)) {
            return selectItems;
        }

        List<MemberCoupon> memberCoupons = memberCouponService.findByMember((Member) HibernateUtils
                .resolveRealObject(customer));

        int a = 0;
        int b = 0;
        for (MemberCoupon memberCoupon : memberCoupons) {
            if (memberCoupon.getCouponType() == MemberCouponType.A) {
                a += memberCoupon.getAmount();
            }
            else if (memberCoupon.getCouponType() == MemberCouponType.B) {
                b += memberCoupon.getAmount();
            }
        }
        double amount = order.getOldAmount();

        for (int i = 100; i < a & amount > 0; i += 100, amount -= 400) {
            MemberCoupon coupon = new MemberCoupon();
            coupon.setId(MemberCouponType.A + ":" + i);
            coupon.setAmount(i);
            coupon.setCouponType(MemberCouponType.A);
            selectItems.add(new SelectItem(coupon, "A卷" + i));
        }
        if (order.getOldAmount() >= 1000 & b >= 300) {
            MemberCoupon coupon = new MemberCoupon();
            coupon.setId(MemberCouponType.B + ":" + 300);
            coupon.setAmount(300);
            coupon.setCouponType(MemberCouponType.B);
            selectItems.add(new SelectItem(coupon, "B卷" + 300));
        }

        if (oldCoupon != null) {
            if (oldCoupon != null && oldCoupon.getAmount() > 0) {
                oldCoupon.setId(oldCoupon.getCouponType() + ":" + oldCoupon.getAmount());
                selectItems.add(new SelectItem(oldCoupon, oldCoupon.getCouponType() + "卷" + oldCoupon.getAmount()));
            }
        }

        return selectItems;
    }
}
